﻿using Furion;
using Furion.Schedule;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Furion.DynamicApiController;
using Furion.RemoteRequest;
using Furion.RemoteRequest.Extensions;
using Furion.Templates;

Serve.Run("http://0.0.0.0:8589");

public class startAppInfo : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRemoteRequest();
    }
}

[DynamicApiController]
public class HelloService
{
    public async Task<string> Say()
    {
        var ss = await "http://www.baidu.com".OnResponsing((client, res) =>
        {
            GloablHttpRemoteAction.GloablRemoteResponse(client, res);
        }).GetAsStringAsync();
        return ss;
    }

    public async Task<string> PostXXXAsync()
    {
        var a = App.GetService<IHttp>();
        var result = await a.PostXXXAsync();
        return result.Content.ToString();
    }
}

public static class GloablHttpRemoteAction
{
    public static void GloablRemoteResponse(HttpClient a, HttpResponseMessage r)
    {
        Console.WriteLine(TP.Wrapper("输出信息", "http拦截", $"##请求地址## {r.RequestMessage.RequestUri.ToString()}", $"##返回信息## {r.Content.ReadAsStringAsync().Result}"));
    }
}

public interface IHttp : IHttpDispatchProxy
{
    // 通过参数拦截
    [Get("http://www.baidu.com")]
    Task<HttpResponseMessage> PostXXXAsync(
        [Interceptor(InterceptorTypes.Exception)]
        Action<HttpClient, HttpResponseMessage, string> action = default);


    // 全局拦截，类中每一个方法都会触发
    [Interceptor(InterceptorTypes.Response)]
    static void OnResponsing1(HttpClient client, HttpResponseMessage res)
    {
        Console.WriteLine(TP.Wrapper("输出信息", "http拦截", $"##拦截头## {res.RequestMessage.Method.ToString()}"));
    }
}